package com.ct.demo;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
//@RestController
@RequestMapping("/response")
public class ResponseController {
    @RequestMapping("/returnPage")
    public String returnPage () {
        return "/hello.html";
    }

    @ResponseBody
    @RequestMapping("/returnData")
    public String returnData () {
        return "/hello.html";
    }

    @ResponseBody
    @RequestMapping("/returnHtml")
    public String returnHtml () {
        return "<h1>我是一级标题</h1>";
    }

    @ResponseBody
    @RequestMapping("/returnJson")
    public List<Person> returnJson () {
        List<Person> people = new ArrayList<>();
        Person person = new Person("zhangsan", "123456", 16);
        people.add(person);
        return people;
    }

    @ResponseBody
    @RequestMapping("/returnMap")
    public Map<String, String> returnMap () {
        Map<String, String> map = new HashMap<>();
        map.put("k1", "v1");
        map.put("k2", "v2");
        map.put("k3", "v3");
        map.put("k4", "v4");
        map.put("k5", "v5");
        return map;
    }

    @ResponseBody
    @RequestMapping("/setStatus")
    public String setStatus (HttpServletResponse response) {
        response.setStatus(400);
        return "设置状态码成功";
    }

    @ResponseBody
    @RequestMapping(value = "/setContentType", produces = "application/json")
    public String setContentType (HttpServletResponse response) {
        return "{\"ok\" : 1}";
    }

}
